<script>
	import {
		http
	} from '@/utils/commonRequest.js'; // 局部引入
	import promise from '@/utils/promise.js'
	import {
		baseImgUrl,
		baseUrl
	} from '@/utils/constant.js'

	export default {
		onLaunch: async function(options) {
			console.log('小程序接收参数=options===',options)
			let query = options.query;
			if(query && query.memberId){
				uni.setStorageSync("login_memberId", query.memberId);
			}
			if(options.memberId){
				uni.setStorageSync("login_memberId", options.memberId);
			}
			console.log("系统信息：", uni.getSystemInfoSync())
			uni.removeStorageSync("LOGIN_LASTTIME");
			uni.removeStorageSync("memberInfo");
			await this.init(options)
			console.log('App Launch')
		},
		
		onShow: function(options) {
			console.log('App Show')
			console.log('应用启动路径：', options.path)
		},
		onHide: function() {
			console.log('App Hide')
			uni.removeStorageSync('outlink')
		},
		methods: {
			async init(options) {
				//获取appid
				this.globalData.appid = uni.getAccountInfoSync().miniProgram.appId;
				//获取设备信息
				this.globalData.sysInfo = uni.getSystemInfoSync();
				// #ifdef MP-WEIXIN
				this.checkVersion()
				await this.initPlatWX()
				// #endif
				await this.getPlatformConfig()
				this.$isResolve()
				//通过分享进入小程序时，先检查是否认证，未认证跳转认证
				if(options.query && options.query.outlink) {
				  await this.getOpenId(options.query.outlink)
				} 
			},
			
			// 检查线上版本,初始化信息
			checkVersion() {
				console.log("============检查线上版本,初始化信息==========================")
				//新版本检测
				const updateManager = uni.getUpdateManager();
				updateManager.onUpdateReady(() => {
					uni.showModal({
						title: '更新提示',
						content: '新版本已经准备好，重启应用立即体验吧！',
						showCancel: false,
						confirmColor: "#59A5F0",
						success: res => {
							// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
							updateManager.applyUpdate()
						}
					})
				});				
				//加载时先登录失效
				uni.setStorageSync("LOGIN_LOGIN", "expire");
				//获取胶囊信息
				this.globalData.menu = uni.getMenuButtonBoundingClientRect()
			},
			
			initPlatWX() {
				uni.setStorageSync("LOGIN_LOGIN", "expire");
				uni.removeStorageSync("LOGIN_LASTTIME");
				uni.removeStorageSync("LOGIN_JSESSIONID");
				uni.removeStorageSync("LOGIN_CSRFTOKEN");
				var that = this
				return new Promise((resolve, reject) => {
					uni.login({
						success: res => {
							let code = res.code
							let login_memberId = uni.getStorageSync('login_memberId');
							let dataObj = {
								code: code,
								appid: uni.getAccountInfoSync().miniProgram.appId
							}
							if(login_memberId){
								dataObj.memberId = login_memberId;
							}
							wx.request({
								url: baseUrl + '/miniapp/loginNotAuthPlat',
								data: dataObj,
								success: res => {
									console.log("===================app.vue 获取平台信息----==========================")
									if (res.data.login == "login" || res.data.login == "authed") {
										wx.setStorageSync("LOGIN_LOGIN", res.data.login);
										wx.setStorageSync("LOGIN_LASTTIME", new Date());
										wx.setStorageSync("LOGIN_JSESSIONID", res.data.JSESSIONID);
										wx.setStorageSync("openid", res.data.openid);
										uni.setStorageSync('memberInfo', res.data.memberInfo)
										uni.setStorageSync('noApproveFlag',res.data.memberInfo && res.data.memberInfo.approve != '1')
										that.globalData.memberInfo = (res.data.login =="authed") ? res.data.memberInfo : null;
										that.globalData.platId = (res.data.login == "authed") ? res.data.platId : null
										uni.setStorageSync("platType", res.data.platType)
										that.globalData.platType = res.data.platType;
										uni.setStorageSync("platName", res.data.platName || null)
										console.log("app.vue that.globalData:", that.globalData)
										console.log("res.data.memberInfo:", res.data.memberInfo)
										resolve(res.data.login);
									} else {
										reject("failed");
									}
								},
								fail: (err) => {
									console.log(err);
									reject("failed");
								}
							})
						},
						fail: function(err) {
							console.log(err);
							reject("failed");
						}
					})
				})
			},
			
			//获取平台配置信息
			async getPlatformConfig() {
				var res = await this.$http.get('/platform/menuConfig')
				// console.log('app vue /platform/menuConfig', res.data)
				if (200 == res?.data?.code) {
					var configDefault = this.globalData.platformConfig;
					var platformConfig = {};
					const config = res.data;
					platformConfig = Object.assign(configDefault, config);
					platformConfig.baseColor = config.cssColor;
					platformConfig.backgroundColor = config.cssColor2;
					if (config.cssColor3) {
						platformConfig.gradientColors = JSON.parse(config.cssColor3);
						platformConfig['gradientColors1'] = utils.hexToRgba(platformConfig.gradientColors[0], 0.1);
						platformConfig['gradientColors2'] = utils.hexToRgba(platformConfig.gradientColors[1], 0.1);
						platformConfig['gradientColors3'] = utils.hexToRgba(platformConfig.gradientColors[0], 0.2);
						platformConfig['gradientColors4'] = utils.hexToRgba(platformConfig.gradientColors[1], 0.2);
					}
					uni.setStorageSync("PLATFORM_CONFIG", platformConfig)
					if (platformConfig.indexStyleType) {
						this.globalData.showPageIndex = platformConfig.indexStyleType == -1 ? 1 : platformConfig.indexStyleType;
					}
					this.globalData.platformConfig = platformConfig
				}
			},
			//根据MiniOpenId是否有值，判断是否认证登录
			getOpenId(shareUrl) {
				return new Promise((resolve, reject) => {
					if(shareUrl) {
						let navUrl = encodeURIComponent(shareUrl);
						uni.setStorageSync('outlink',navUrl)
						http.get('/taiHuiWan/getMemberUserMiniOpenId', {}).then(res => {
							if(res.data.code == 500) {
								uni.switchTab({
									url: '/pages/my/my'
								})
							} else {
								uni.setStorageSync('MiniOpenId', res.data.MiniOpenId)
								uni.navigateTo({
									url: '/pages/outlink/outlink?outlink=' + navUrl,
								})
							}
							resolve()
						})
					}
			    })
			}, 
		},
		globalData: {
			baseImageUrl: "https://mall.mintaibank.com/frontend/benefits/static/bankStandard/image/zsnsh/",//生产环境图片路径，不需要改
			// baseImageUrl: "https://aw.zjmtbank.com:30001/frontend/benefits/static/bankStandard/image/zsnsh/",
			// baseImageUrl: "https://aw.zjmtbank.com:30002/frontend/benefits/static/bankStandard/image/zsnsh/",
			// baseImageUrl: "https://aw.zjmtbank.com:30003/frontend/benefits/static/bankStandard/image/zsnsh/",
			baseurl: baseUrl,
			jwt: null,
			currLatitude: '31.245105',
			currLongitude: '121.506377',
			platLat: null,
			platLng: null,
			cityCode: '3101',
			defaultCity: '上海市',
			defaultCounty: '上海市',
			locatedCity: '上海市',

			appid: 'wxafea20932a430358',
			sysInfo: null, //设备信息
			userInfo: null,

			JSESSIONID: null,
			memberInfo: null,
			platId: null,
			platType: null, //平台类型 1标准，2无区域总分，3有区域总分
			platFlag: 1, // 是否切换了平台 1为否，2为是
			cityId: null,

			sysInfo: {}, //设备信息
			hasToRegister: false, //是否已去过注册页面
			csrfToken: null,

			inviterId: null, //邀请者ID
			inviteActId: null, //邀请有礼活动ID
			MAX_INACTIVE_INTERVAL: 60 * 10, //session失效时长，s
			areaData: null, //存放省市区的数据

			platformConfig: {
				local: true, //小程序本地默认设置，从后台获取后无该属性
				baseColor: '#FF6464', //主配色
				backgroundColor: '#FFEBEB', //背景色
				gradientColors: ['#F27B9D', '#F54973'], // 渐变色
				activitySwitch: '0', //活动开关，0关闭，1开启
				couponSwitch: '0', //领劵开关，0关闭，1开启
				supportSwitch: '0', //技术支持开关，0关闭，1开启
				model: '1' //商城模式，1单店，2多店
			},
			menu: null,
			dicType: null, //任务类型
			dictId: null, //任务字典id
			taskId: null, //任务id
			eventId: null, //事件id
			contentType: null, //内容类型
			eventType: null, //事件类型
			taskTimer: null, //任务计时器

			/**
			 * 优惠券订单支付
			 */
			toCouponPay: function(payId, amount, orderType, callBack) {
				let that = this;
				// #ifdef H5 || MP-UNIONPAY
				let countdown;
				let orderId;
				http.get('/zsnshCoupon/getPay', {
					params: {
						payId: payId
					}
				}).then((res) => {
					console.log(res);
					let amount = res.data.amount;
					countdown = res.data.time;

					if (undefined != countdown && countdown <= 0) {
						uni.hideLoading();
						uni.showToast({
							title: '订单超时',
							icon: 'none'
						});
						return;
					}
					let len = payId.length;
					let code = payId.substring(len - 3, len);
					let divisionFlag = res.data.divisionFlag || false;
					let subOrders = res.data.subOrders || [];
					let tid = res.data.tid || ''
					// #endif
					// #ifdef MP-UNIONPAY
					let upParams = {
						orderId: payId,
						tid: tid,
						bmsDeviceId: '22222222',
						msgType: 'uac.appOrder',
						codeType: code
					}
					http.get('orderPay/makeOrder', { //通过网付进行支付下单，获取支付参数
						params: upParams
					}).then(res => {
						uni.hideLoading();
						res = res.data
						console.log("unionpay pay res:", res)
						if (200 == res.code) {
							var data = res.data;
							var appPayRequest = data.appPayRequest;
							var errCode = data.errCode;
							if ("SUCCESS" != errCode) {
								var errMsg = data.errMsg;
								uni.showToast({
									title: errMsg
								})
							}
							var tn = appPayRequest.tn;
							var merOrderId = data.merOrderId;
							if ("" == tn) {
								uni.showToast({
									title: "未成功获取支付tn号",
									icon: 'none'
								})
							}
							upsdk.pluginReady(function() {
								upsdk.pay({
									tn: tn,
									success: function() {
										uni.redirectTo({
											url: '/pages/coupon/myCouponDetail/myCouponDetail?payId=' +
												payId
										});
									},
									fail: function(err) {
										console.log(err);
										uni.showToast({
											title: "支付失败" + err.msg,
											icon: "none"
										})
									}
								});
							});
						} else {
							uni.showToast({
								title: res.msg,
								duration: 3000,
								icon: 'none'
							})
						}
					}).catch(err => {
						console.log(err);
						uni.hideLoading();
					});
					// #endif
					// #ifdef H5
					let UA = window.navigator.userAgent.toLowerCase();
					if (UA.indexOf('unionpay') > 0) {
						return
					}
					http.post('orderPay/wxPay', { //通过网付进行支付下单，获取支付参数
						orderId: payId,
						tid: tid,
						bmsDeviceId: '22222222',
						codeType: code,
						divisionFlag: divisionFlag,
						subOrders: JSON.stringify(subOrders),
						// returnUrl: '/frontend/benefits/bankStandard/pages/coupon/myCouponDetail/myCouponDetail?payId=' + payId
						returnUrl: '/frontend/benefits/bankStandard/static/h5/returnUrl/returnUrl.html?pageType=couponOrder&orderId=' +
							payId + '&orderType=' + orderType
					}, {
						header: {
							"content-type": "application/x-www-form-urlencoded"
						}
					}).then((res) => {
						console.log('res', res)
						uni.hideLoading();
						if (200 == res.data.code) {
							window.location.href = res.data.url
						} else if ('ALREADY_PAYED' == res.data.msg) {
							uni.hideLoading();
							utils.message('请勿重复支付', () => {});
						} else {
							uni.showToast({
								title: res.data.msg || '支付失败',
								icon: 'none'
							});
						}
					}).catch((err) => {
						console.log(err);
						uni.hideLoading();
						uni.showToast({
							title: '支付失败',
							icon: 'none'
						});
					});
				})
				// #endif
				// #ifdef MP-WEIXIN
				http.get(
					'/zsnshCoupon/pay', {
						params: {
							payId: payId
						}
					}, {
						header: {
							"content-type": "application/x-www-form-urlencoded"
						}
					}
				).then((res) => {
					res = res.data
					if (200 == res.code) {
						uni.hideLoading();
						let payParams = JSON.parse(res.payParams);
						var netpayOrderId = res.netpayOrderId;
						console.log(payParams);
						uni.requestPayment({
							//调起微信支付
							timeStamp: payParams.timeStamp,
							nonceStr: payParams.nonceStr,
							package: payParams.package,
							signType: payParams.signType,
							paySign: payParams.paySign,
							success: (res) => {
								that.queryResult(netpayOrderId, payId, orderType, callBack);
								that.consumeTaskLog(amount);
							},
							fail: (res) => {
								if ('requestPayment:fail cancel' == res.errMsg) {
									setTimeout(function() {
										uni.redirectTo({
											url: '/pages/coupon/myCouponDetail/myCouponDetail?payId=' +
												payId
										});
									}, 100);
								} else {
									console.log(res);
								}
							}
						});
					} else if ('ALREADY_PAYED' == res.msg) {
						uni.hideLoading();
						utils.message('请勿重复支付', () => {});
					} else {
						uni.hideLoading();
					}
				});
				// #endif
			},
			/**
			 * 查询订单支付状态
			 * @param {*} payId
			 */
			queryResult: function(netpayOrderId, payId, orderType, callBack) {
				let that = this;
				setTimeout(() => {
					that.queryResultLoop(netpayOrderId, payId, orderType, 0, callBack);
				}, 2000);
			},

			/**
			 * 循环查询支付结果，最多查询三次
			 * @param {*网付订单id} netpayOrderId
			 * @param {*自有系统订单id} payId
			 * @param {*订单类型，orderType='groupBuy'为拼团订单，支付成功后跳转至拼团详情页} orderType
			 * @param {*重试次数} time
			 */
			queryResultLoop: function(netpayOrderId, payId, orderType, time, callBack) {
				let that = this;

				if (time >= 3) {
					uni.hideLoading();
					utils.message('优惠券领取中，请以实际领取情况为准！', () => {
						uni.redirectTo({
							url: '/pages/coupon/myCouponList/myCouponList'
						});
					});
				} else {
					http.get('/zsnshCoupon/payResult', {
						params: {
							netpayOrderId: netpayOrderId
						}
					}).then((res) => {
						res = res.data
						if (200 == res.code) {
							uni.hideLoading();
							uni.showToast({
								title: '支付成功',
								duration: 3000
							});

							if (typeof callBack === 'function') {
								callBack();
							}

							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/payResult/payResult?payId=' + payId +
										'&orderType=' + orderType
								});
							}, 2000);
						} else {
							setTimeout(() => {
								that.queryResultLoop(netpayOrderId, payId, orderType, ++time,
									callBack);
							}, 3000);
						}
					});
				}
			},
			consumeTaskLog(consumeAmt) {
				let that = this;
				let params = {
					busCode: '1',
					consumeAmt: consumeAmt,
					dictId: ''
				};
				http.get('task/addLog', {
						params: params
					})
					.then((res) => {
						res = res.data
						if (res.code == '200') {
							console.log('消费任务完成');
						}
					})
					.catch((err) => {
						console.log(err);
					});
			},

			/**
			 * 获取用户地理位置
			 */
			getUserLocation: function() {
				let that = this;
				// #ifndef MP-UNIONPAY
				console.log("非云闪付编译模式")
				return new Promise((resolve, inject) => {
					uni.getLocation({
						type: 'wgs84',
						success(res) {
							console.log('获取用户地理位置',res);
							const latitude = res.latitude;
							const longitude = res.longitude;
							that.currLongitude = longitude;
							that.currLatitude = latitude;
						},
						complete() {
							resolve("end")
						}
					});
				})
				// #endif
				// #ifdef MP-UNIONPAY
				console.log("云闪付编译模式")
				upsdk.pluginReady(function() {
					upsdk.getLocationGps({
				  success: function(res) {
							var userLocation;
							if ("string" === typeof res) {
								userLocation = JSON.parse(res)
							}
							that.currLongitude = userLocation.longitude;
							that.currLatitude = userLocation.latitude;
							console.log("云闪付获取用户经纬度成功：", userLocation)
						},
						fail: function(err) {
							console.log("云闪付获取用户经纬度失败：", err)
						}
					});
				});
				// #endif

			},

			/**
			 * 获取平台配置信息
			 */
			getPlatformConfig: function() {
				let that = this;
				return new promise((resolve, reject) => {
					http.get('/platform/menuConfig', {
							params: {}
						})
						.then((res) => {
							if (200 == res.data.code) {
								const config = res.data;
								that.platId = config.platId || ''
								that.platformConfig = Object.assign(that.platformConfig, config);
								that.platformConfig.baseColor = config.cssColor;
								that.platformConfig.backgroundColor = config.cssColor2;
								if (config.cssColor3) {
									that.platformConfig.gradientColors = JSON.parse(config.cssColor3);
								}
								resolve(that.platformConfig);
							} else {
								resolve(that.platformConfig);
							}
						})
						.catch((err) => {
							resolve(that.platformConfig);
						});
				});
			},

			updateTabBarSkin: function() {
				const baseColor = this.platformConfig.baseColor;
				uni.setTabBarStyle({
					selectedColor: baseColor
				});
				for (let i = 0; i < 4; i++) {
					// console.log(`static/image/tabbar/${baseColor.substring(1)}/${i}_sel.png`)
					uni.setTabBarItem({
						index: i,
						selectedIconPath: `static/image/tabbar/${baseColor.substring(1)}/${i}_sel.png`
					});
				}
			},

			/**
			 * 任务完成状态提交
			 */
			taskLog(type, dictId, taskId, share) {
				let that = this;
				let dicType = (Number(type) + 1).toString();
				http.get('task/addLog', {
						params: {
							busCode: dicType,
							dictId: dictId,
							taskId: taskId,
							consumeAmt: ''
						}
					}).then((res) => {
						console.log(res);

						if (res.data.code == '200') {
							if (util.isEmpty(share)) {
								uni.showToast({
									title: '任务完成',
									icon: 'success'
								});
							}
						}
					})
					.catch((err) => {
						console.log(err);
					});
			},
			payRes: function(tips, orderId, needRedirect, callBack) {
				if (null == tips) {
					if (needRedirect) {
						setTimeout(function() {
							// uni.redirectTo({
							//   url: '/subPackages/orderPkg/pages/orderDetail/orderDetail?orderId=' + orderId
							// })
							uni.redirectTo({
								url: '/subPackages/mall/pages/payResult/payResult?orderId=' + orderId,
							})
						}, 100);
					} else {
						uni.showLoading({
							title: '加载中...',
							mask: true
						})
						setTimeout(function() {
							callBack();
						}, 2000);
					}
				} else {
					uni.showToast({
						title: tips,
						icon: 'none',
						duration: 3000,
						mask: true,
						complete: () => {
							if (needRedirect) {
								setTimeout(function() {
									// uni.redirectTo({
									//   url: '/subPackages/orderPkg/pages/orderDetail/orderDetail?orderId=' + orderId
									// })
									uni.redirectTo({
										url: '/subPackages/mall/pages/payResult/payResult?orderId=' +
											orderId,
									})
								}, 3000);
							} else {
								setTimeout(function() {
									callBack();
								}, 2000);
							}
						}
					})
				}
			},
			consumeTaskLog(consumeAmt) {
				let that = this;
				let params = {
					busCode: '1',
					consumeAmt: consumeAmt,
					dictId: '',
				}
				http.get("task/addLog", {
					params: params
				}).then(res => {
					res = res.data
					if (res.code == '200') {
						console.log('消费任务完成');
					}
				}).catch(err => {
					console.log(err);
				})
			},
			/**
			 * 支付
			 */
			toPay: function(payId, needRedirect, callBack) {
				let that = this;
				let netpayOrderId = null;
				let countdown;
				let orderId;
				let tid;
				http.post('/shopPlat/getPay', {
					payId: payId
				}, {
					header: {
						"content-type": "application/x-www-form-urlencoded"
					}
				}).then(res => {
					console.log(res);
					let amount = res.data.amount;
					countdown = res.data.time;
					orderId = res.data.subOrders[0].orderId;
					let tid = res.data.tid;
					if (undefined != countdown && countdown <= 0) {
						that.payRes('订单超时', orderId, needRedirect, callBack);
						return;
					}
					if (parseFloat(res.data.amount) < 0.01) {
						that.payRes(null, orderId, needRedirect, callBack);
						return;
					}
					//支付code取后三位
					let len = payId.length;
					let code = payId.substring(len - 3, len);
					let subOrders = res.data.subOrders;
					console.log("即将进入支付流程")
					// #ifdef MP-WEIXIN
					console.log("进入微信小程序支付")
					http.post('miniapp/pay2', { //通过网付进行支付下单，获取支付参数
						orderId: payId,
						tid: tid,
						code: code,
						divisionFlag: true,
						subOrders: JSON.stringify(subOrders)
					}, {
						header: {
							"content-type": "application/x-www-form-urlencoded"
						}
					}).then(res => {
						uni.hideLoading();
						if (200 == res.data.code) {
							console.log(res);
							let payParams = JSON.parse(res.data.payParams);
							netpayOrderId = res.data.netpayOrderId;
							uni.requestPayment({ //调起微信支付
								timeStamp: payParams.timeStamp,
								nonceStr: payParams.nonceStr,
								package: payParams.package,
								signType: payParams.signType,
								paySign: payParams.paySign,
								success: res => {
									http.post('/miniapp/payResult', {
										orderId: netpayOrderId,
										code: code,
										payId: payId,
										prepayId: payParams.package
									}, {
										header: {
											"content-type": "application/x-www-form-urlencoded"
										}
									});
									that.payRes(null, orderId, needRedirect, callBack);
									if (amount > 0) {
										that.consumeTaskLog(amount);
									}
								},
								fail: res => {
									if ('requestPayment:fail cancel' == res.errMsg) {
										that.payRes(null, orderId, needRedirect, callBack);
									} else {
										console.log(res);
										that.payRes('支付失败', orderId, needRedirect,callBack);
									}
								}
							})
						} else if ("ALREADY_PAYED" == res.data.msg) {
							that.payRes('重复支付', orderId, needRedirect, callBack);
						} else if ("MERCHANT_CONFIG_EXCEPTION" == res.data.msg) {
							that.payRes('商家支付参数错误，请联系商家处理', orderId, needRedirect, callBack);
						} else if ("MERCHANT_MID_NULL" == res.data.msg) {
							that.payRes('商家未设置支付参数，请联系商家处理', orderId, needRedirect, callBack);
						} else if ("MERCHANT_MINMID_NULL" == res.data.msg) {
							that.payRes(res.data.tip, orderId, needRedirect, callBack);
						} else {
							that.payRes('支付失败', orderId, needRedirect, callBack);
						}
					}).catch(err => {
						console.log(err);
						uni.hideLoading();
						that.payRes('支付失败', orderId, needRedirect, callBack);
					});
					// #endif
					// #ifdef MP-UNIONPAY
					console.log("进入云闪付小程序支付")
					let upParams = {
						orderId: payId,
						tid: tid,
						bmsDeviceId: '22222222',
						msgType: 'uac.appOrder',
						codeType: code
					}
					console.log("orderPay/makeOrder upParams:", upParams)
					http.get('orderPay/makeOrder', { //通过网付进行支付下单，获取支付参数
						params: upParams
					}).then(res => {
						console.log("orderPay/makeOrder res", res.data)
						uni.hideLoading();
						res = res.data
						console.log("unionpay pay res:", res)
						if (200 == res.code) {
							var data = res.data;
							var appPayRequest = data.appPayRequest;
							var errCode = data.errCode;
							if ("SUCCESS" != errCode) {
								var errMsg = data.errMsg;
								uni.showToast({
									title: errMsg
								})
							}
							var tn = appPayRequest.tn;
							var merOrderId = data.merOrderId;
							if ("" == tn) {
								uni.showToast({
									title: "未成功获取支付tn号",
									icon: 'none'
								})
							}
							upsdk.pluginReady(function() {
								upsdk.pay({
									tn: tn,
									success: function() {
										that.payRes(null, orderId, needRedirect,callBack);

									},
									fail: function(err) {
										console.log(err)
										uni.showToast({
											title: "支付失败:" + err.msg,
											icon: 'none'
										})
									}
								});
							});
						} else {
							uni.showToast({
								title: res.msg,
								duration: 3000,
								icon: 'none'
							})
						}
					}).catch(err => {
						console.log(err);
						uni.hideLoading();
						that.payRes('支付失败', orderId, needRedirect, callBack);
					});
					// #endif
					// #ifdef H5
					console.log("进入H5支付")
					let UA = window.navigator.userAgent.toLowerCase();
					if (UA.indexOf('unionpay') < 0) {
						http.post('orderPay/wxPay', { //通过网付进行支付下单，获取支付参数
							orderId: payId,
							tid: tid,
							bmsDeviceId: '22222222',
							codeType: code,
							divisionFlag: true,
							subOrders: JSON.stringify(subOrders),
							returnUrl: '/frontend/benefits/bankStandard/static/h5/returnUrl/returnUrl.html?pageType=mall&orderId=' +
								orderId
						}, {
							header: {
								"content-type": "application/x-www-form-urlencoded"
							}
						}).then(res => {
							console.log(res, 89898)
							uni.hideLoading();
							if (200 == res.data.code) {
								console.log(window.location, 'window.location')
								window.location.replace(res.data.url)
							} else if ("ALREADY_PAYED" == res.data.msg) {
								that.payRes('重复支付', orderId, needRedirect, callBack);
							} else if ("MERCHANT_CONFIG_EXCEPTION" == res.data.msg) {
								that.payRes('商家支付参数错误，请联系商家处理', orderId, needRedirect, callBack);
							} else if ("MERCHANT_MID_NULL" == res.data.msg) {
								that.payRes('商家未设置支付参数，请联系商家处理', orderId, needRedirect, callBack);
							} else if ("MERCHANT_MINMID_NULL" == res.data.msg) {
								that.payRes(res.data.tip, orderId, needRedirect, callBack);
							} else {
								console.log(res.data.msg, 99)
								that.payRes('支付失败', orderId, needRedirect, callBack);
							}
						}).catch(err => {
							console.log(err);
							uni.hideLoading();
							that.payRes('支付失败', orderId, needRedirect, callBack);
						});
					}
					// #endif
				})
			},
			payRes: function(tips, orderId, needRedirect, callBack) {
				if (null == tips) {
					if (needRedirect) {
						setTimeout(function() {
							uni.redirectTo({
								url: '/subPackages/mall/pages/payResult/payResult?orderId=' + orderId,
							})
						}, 100);
					} else {
						uni.showLoading({
							title: '加载中...',
							mask: true
						});
						setTimeout(function() {
							callBack();
						}, 2000);
					}
				} else {
					uni.showToast({
						title: tips,
						icon: 'none',
						duration: 3000,
						mask: true,
						complete: () => {
							if (needRedirect) {
								setTimeout(function() {
									uni.redirectTo({
										url: '/subPackages/mall/pages/payResult/payResult?orderId=' +
											orderId
									});
								}, 3000);
							} else {
								setTimeout(function() {
									callBack();
								}, 2000);
							}
						}
					});
				}
			},
		}
	}
</script>

<style lang="scss">
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "uview-ui/index.scss";
	// uviewUi内置样式引入
	@import "uview-ui/libs/css/flex.scss";
	@import 'iconfont/iconfont.css';

	.box {
		width: 694rpx;
		margin-left: 28rpx;
		padding: 28rpx;
		/* height: 280px; */
		background: #FFFFFF;
		box-shadow: 0rpx 12rpx 34rpx 0rpx rgba(182, 178, 199, 0.2);
		border-radius: 16rpx;
	}
	
	.u-safe-bottom{
		display: none !important;
	}

	image {
		width: 100%;
		height: 100%;
	}

	page {
		-webkit-text-size-adjust: 100% !important;
		font-size: 28rpx;
		background-color: #f8f8f8;
		font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", Helvetica,
			Segoe UI, Arial, Roboto, "PingFang SC", "miui", "Hiragino Sans GB",
			"Microsoft Yahei", sans-serif;
	}

	.page.tabbar-page {
		padding-bottom: 48px;
		padding-bottom: calc(48px + env(safe-area-inset-bottom));
	}

	view,
	image,
	text,
	navigator,
	scroll-view {
		margin: 0;
		padding: 0;
		box-sizing: border-box;
	}

	button,
	button::after {
		outline: none;
		border: none;
	}

	.ellipsis {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.ellipsis2 {
		display: -webkit-box;
		overflow: hidden;
		text-overflow: ellipsis;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}
</style>
